package com.smasher.backup.dialog.bottomsheet;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.smasher.backup.dialog.R;
import com.smasher.widget.listener.OnItemClickListener;

import java.util.List;

/**
 * @author HXZ_Android
 */
public abstract class BaseBottomSheetListDialog extends BaseBottomSheetDialogFragment
        implements View.OnClickListener, OnItemClickListener {


    private DialogBottomSheetAdapter mAdapter;
    private OnSelectListener mSelectListener;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new BottomSheetDialog(requireContext(), R.style.DefinedBottomSheetStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    /**
     * can use{@link ViewGroup.LayoutParams#MATCH_PARENT}
     * or {@link ViewGroup.LayoutParams#WRAP_CONTENT}
     * or exactly number
     */
    @Override
    protected int getWidth() {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }

    /**
     * can use{@link ViewGroup.LayoutParams#MATCH_PARENT}
     * or {@link ViewGroup.LayoutParams#WRAP_CONTENT}
     * or exactly number
     */
    @Override
    protected int getHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_bottom_sheet_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mAdapter = new DialogBottomSheetAdapter();
        mAdapter.setOnItemClickListener(this);
        mAdapter.setData(buildList());

        TextView mTvTitle = view.findViewById(R.id.tvTitle);
        TextView mTvCancel = view.findViewById(R.id.tvCancel);
        RecyclerView mList = view.findViewById(R.id.recycler_view);

        mTvTitle.setText(R.string.dialog_select);
        mTvCancel.setText(R.string.dialog_cancel);

        mList.setAdapter(mAdapter);
        mList.setLayoutManager(new LinearLayoutManager(getContext()));
        mList.addItemDecoration(getDividerItemDecoration());

        mTvCancel.setOnClickListener(this);
    }

    public abstract List<BeanDialogItem> buildList();

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        dismiss();
    }

    public DividerItemDecoration getDividerItemDecoration() {
        Context context = requireContext();
        Drawable drawable =
                ContextCompat.getDrawable(context, R.drawable.divider_line_style_color_b9b9b9);
        DividerItemDecoration decoration =
                new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
        if (drawable != null) {
            decoration.setDrawable(drawable);
        }
        return decoration;
    }

    @Override
    public void onClick(View view, int i) {
        BeanDialogItem item = mAdapter.getItem(i);
        if (mSelectListener != null) {
            mSelectListener.onSelect(item);
        }
    }

    @Override
    public void onLongClick(View view, int i) {

    }

    @Override
    public void onEditFinish(View view, int i, String s) {

    }

    public void setSelectListener(OnSelectListener mSelectListener) {
        this.mSelectListener = mSelectListener;
    }

    public interface OnSelectListener {
        void onSelect(BeanDialogItem item);
    }

}
